package com.design.springboot.booksharing.bean;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.validation.constraints.NotNull;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * sql 参数的包裹
 *
 * @author admin
 */
public class QueryWarp<T> implements Map<String, Object>{
    private final Map<String, Object> map;

    public QueryWarp() {
        this.map = new HashMap<>();
    }

    public QueryWarp(@NotNull T o) {
        this();
        this.put(o);
    }

    public QueryWarp<T> put(T o) {
        String jsonStr = JSONObject.toJSONString(o, SerializerFeature.WriteMapNullValue);
        this.map.putAll(JSON.parseObject(jsonStr, Map.class));
        return this;
    }

    @Override
    public int size() {
        return this.map.size();
    }

    @Override
    public boolean isEmpty() {
        return this.map.isEmpty();
    }

    @Override
    public boolean containsKey(Object key) {
        return this.map.containsKey(key);
    }

    @Override
    public boolean containsValue(Object value) {
        return this.map.containsValue(value);
    }

    @Override
    public Object get(Object key) {
        return this.map.get(key);
    }

    @Override
    public QueryWarp<T> put(String key, Object value) {
        this.map.put(key, value);
        return this;
    }

    @Override
    public Object remove(Object key) {
       return this.map.remove(key);
    }

    @Override
    public void putAll(Map<? extends String, ?> m) {
        this.map.putAll(m);
    }

    @Override
    public void clear() {
        this.map.clear();
    }

    @Override
    public Set<String> keySet() {
        return this.map.keySet();
    }

    @Override
    public Collection<Object> values() {
        return this.map.values();
    }

    @Override
    public Set<Entry<String, Object>> entrySet() {
        return this.map.entrySet();
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this.map);
    }
}
